package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.WorkCommentDao;
import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.UserNotice;
import com.ketangpai.admin.entity.Work;
import com.ketangpai.admin.entity.WorkComment;
import com.ketangpai.admin.entity.dto.WorkCommentDTO;
import com.ketangpai.admin.entity.param.Param;
import com.ketangpai.admin.service.UserNoticeService;
import com.ketangpai.admin.service.UserService;
import com.ketangpai.admin.service.WorkCommentService;
import com.ketangpai.admin.service.WorkService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作业评论表(WorkComment)表服务实现类
 *
 * @author makejava
 * @since 2023-06-14 01:17:52
 */
@Service("workCommentService")
@Transactional
public class WorkCommentServiceImpl extends ServiceImpl<WorkCommentDao, WorkComment> implements WorkCommentService {
    @Resource
    private WorkCommentDao workCommentDao;

    @Resource
    private WorkService workService;
    @Resource
    private UserNoticeService userNoticeService;
    @Resource
    private UserService userService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    public WorkComment queryById(Integer id) {
        return workCommentDao.selectById(id);
    }

    /**
     * 通过作业ID查询评论数据
     */
    public List<WorkComment> queryByWorkId(Integer id) {
        QueryWrapper<WorkComment> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id", id);
        return workCommentDao.selectList(wrapper);
    }

    /**
     * 分页查询
     */
    @Override
    public Map<String, Object> selectList(Param param) {
        List<WorkComment> workComments = queryByWorkId(param.getWorkId());
        List<WorkCommentDTO> list = treeList(workComments);

        int startIndex = (param.getPage() - 1) * param.getPageSize();
        int endIndex = Math.min(startIndex + param.getPageSize(), list.size());
        List<WorkCommentDTO> dtoList = list.subList(startIndex, endIndex);//分页
        Map<String, Object> map = new HashMap<>();
        map.put("allTotal", workComments.size());
        map.put("total", list.size());
        map.put("list", dtoList);
        return map;
    }

    /**
     * 树形结构
     */
    public List<WorkCommentDTO> treeList(List<WorkComment> menuList) {
        return menuList.stream()
                .filter(menu -> menu.getSuperiorId().equals(0))//筛选最上级的数据
                .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList());
    }

    /**
     * 设置children属性
     */
    public WorkCommentDTO covertMenuNode(WorkComment menu, List<WorkComment> menuList) {
        WorkCommentDTO node = new WorkCommentDTO();
        BeanUtils.copyProperties(menu, node);//浅拷贝
        List<WorkCommentDTO> children = menuList.stream()
                .filter(subMenu -> subMenu.getSuperiorId().equals(menu.getId()))//筛选下一级的数据
                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
        User user = userService.selectById(menu.getUserId());
        node.setUserName(user.getName());//当前姓名
        node.setAvatar(user.getAvatar());//当前头像

//        if(menu.getRespondentId()!=null){
//            User user1=userService.selectById(menu.getRespondentId());
//            node.setRespondentName(user1.getName());//回复人姓名
//        }
        if (menu.getRespondentId() != null) {
            System.out.println("查询的 respondentId: " + menu.getRespondentId());
            User user1 = userService.selectById(menu.getRespondentId());
            if (user1 != null) {
                node.setRespondentName(user1.getName());
            } else {
                System.out.println("找不到对应的用户: " + menu.getRespondentId());
                node.setRespondentName("未知");
            }
        }

        node.setChildren(children);
        System.out.println(children);
        return node;
    }

    /**
     * 新增数据
     */
    @Override
    public boolean insert(WorkComment workComment) {
        addNotice(workComment);
        return workCommentDao.insert(workComment) > 0;
    }

    /**
     * 用户通知新增
     */
    public void addNotice(WorkComment workComment) {
        Work work = workService.queryById(workComment.getWorkId());
        UserNotice userNotice = new UserNotice();
        userNotice.setUserId(workComment.getUserId());
        userNotice.setActivity(work.getActivity());
        userNotice.setContentId(workComment.getId());
        userNotice.setCourseId(work.getCourseId());
        userNoticeService.save(userNotice);
    }

    /**
     * 修改数据
     */
    @Override
    public boolean update(WorkComment workComment) {
        return workCommentDao.updateById(workComment) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("superior_id", id);//根据上级id删除
        workCommentDao.deleteByMap(map);
        return workCommentDao.deleteById(id) > 0;
    }
}
